package com.skt.common_hr.holotreq_nos.model
{
	import com.skt.common_hr.common.approval.IRequestApprovalProxy;
	import com.skt.common_hr.common.puremvc.CommonProxy;

	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;

	public class DExpenseApprovalProxy extends CommonProxy implements IRequestApprovalProxy
	{
		public static const NAME:String = "DExpenseApprovalProxy";
		public var dataProvider:Object;
		private var dayTitles:Array = ["(일)","(월)","(화)","(수)","(목)","(금)","(토)"];
		private var templateHTML:String = "";
		/**
		 * 생성자
		 *
		 * @param
		 * @return void
		 */
		public function DExpenseApprovalProxy(data:Object=null)
		{
			super(NAME, data);
			loadTemplate();
		}

		private var _key:String = "";
		public function get key():String
		{
			trace("결재 키: " + _key);
			return _key;
		}
		public function set key(k:String):void
		{
			_key = k;
		}

		public function get title():String
		{
			var tmp:String = "석식비 신청(";
			tmp += getFormatDate(dataProvider.I_DATUM) + ")";
			/*
			if(DateUtil.daySpan(dataProvider.BEGDA, dataProvider.ENDDA) > 1 ) {
				tmp += DateUtil.toDefaultString(dataProvider.BEGDA) + " ~ " + DateUtil.toDefaultString(dataProvider.ENDDA) + ")";
			}
			else {
				tmp += DateUtil.toDefaultString(dataProvider.BEGDA) + ")";
			}
			*/
			return tmp;
		}

		private function getFormatDate(date:String):String
		{
			return date.substr(0, 4) + "-" + date.substr(4, 2) + "-" +  date.substr(6, 2);
		}

		public function get content():String
		{
			return buildHTML(dataProvider);
		}

		public function get appCode():String
		{
			return "10"; 	//
		}

 		private function loadTemplate():void
		{
			var request:URLRequest = new URLRequest("assets/template/dexpense.tmp");
			loader.addEventListener(Event.COMPLETE, templateResult);
			loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

            try {
                loader.load(request);
            } catch (error:Error) {
                trace("Unable to load requested document.");
            }
		}

		private function templateResult(event:Event):void
		{
			templateHTML = loader.data;
		}

		private function ioErrorHandler(event:IOErrorEvent):void
		{
			trace("ioErrorHandler: " + event);
		}



		private function buildHTML(appData:Object):String
		{
			var htmlAll:String = templateHTML;
			var htmlBlock:String = "";

			var ptnDate:RegExp = /__DATE__/;
			var ptnText1:RegExp = /__TEXT1__/;
			var ptnNewLine:RegExp = /\r/g;
			htmlAll = htmlAll.replace(ptnDate, getFormatDate(appData.BEGDA) + " " + dayTitles[appData.DAY]);
			htmlAll = htmlAll.replace(ptnText1, appData.TEXT1.replace(ptnNewLine, "<br>"));
			return htmlAll;
		}


	}
}